﻿using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Utilities;
using Process.Entities;
using System.Collections.Generic;

namespace Process.DAL
{
    public class ProductDAL
    {
        private static ProductENT CreateProductFromReader(IDataReader reader)
        {
            ProductENT item = new ProductENT();

            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.ProductID.ToString()))) item.ProductID = Utility.StringParse(reader[ProductColumns.ProductID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.ProductName.ToString()))) item.ProductName = Utility.StringParse(reader[ProductColumns.ProductName.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.ProductType.ToString()))) item.ProductType = Utility.StringParse(reader[ProductColumns.ProductType.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.UnitPrice.ToString()))) item.UnitPrice = Utility.DecimalParse(reader[ProductColumns.UnitPrice.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.CategoryID.ToString()))) item.CategoryID = Utility.StringParse(reader[ProductColumns.CategoryID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.Author.ToString()))) item.Author = Utility.StringParse(reader[ProductColumns.Author.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.ProductImageID.ToString()))) item.ProductImageID = Utility.StringParse(reader[ProductColumns.ProductImageID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.ProductDescription.ToString()))) item.ProductDescription = Utility.StringParse(reader[ProductColumns.ProductDescription.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.IsDeleted.ToString()))) item.IsDeleted = Utility.BoolParse(reader[ProductColumns.IsDeleted.ToString()]);

            DataTable dt = reader.GetSchemaTable();
            if (dt.IsColumnExist(ProductColumns.ProductImage.ToString()))
            {
                if (!reader.IsDBNull(reader.GetOrdinal(ProductColumns.ProductImage.ToString()))) item.ProductImage = Utility.StringParse(reader[ProductColumns.ProductImage.ToString()]);
            }

            return item;
        }

        public static ProductENT GetProductByID(string productID)
        {
            try
            {
                ProductENT product = new ProductENT();

                DataAccessHelper.AddParameter("ProductID", SqlDbType.VarChar, productID);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procProductGetByID"))
                {
                    while (reader.Read())
                    {
                        product = CreateProductFromReader(reader);
                    }
                }

                return product;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin sản phẩm.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        /// <summary>
        /// Get product lít
        /// </summary>
        /// <param name="top">get top of product list. 0 is get all</param>
        /// <returns></returns>
        public static List<ProductENT> GetProductList(int top)
        {
            try
            {
                List<ProductENT> productList = new List<ProductENT>();

                DataAccessHelper.AddParameter("Top", SqlDbType.Int, top);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procProductGetList"))
                {
                    while (reader.Read())
                    {
                        productList.Add(CreateProductFromReader(reader));
                    }
                }

                return productList;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin sản phẩm.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}
